﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

/// <summary>
/// Summary description for Utils
/// </summary>
public static class Utils
{
	

    /// <summary>
    /// To get the connecting string object instatiated so that it can
    /// be used locally in this class
    /// </summary>
    public static SqlConnection getDBConnection()
    {
        try
        {
            //get connection string for config
            string connString = ConfigurationSettings.AppSettings["moviesConnectionString"];
            SqlConnection conn = new SqlConnection(connString);
            return conn;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error getting connection string:" + ex.Message);
        }

        return null;
    }

    /// <summary>
    /// utility method to change a data reader to dataset 
    /// for binding purpose
    /// </summary>
    /// <param name="reader">Data Reader</param>
    /// <returns>The datareader object is returned as equivalent dataset object</returns>
    public static DataSet DataReaderToDataSet(IDataReader reader)
    {
        DataTable table = new DataTable();
        int fieldCount = reader.FieldCount;

        //loop over datareader fields and fill it into datatable
        for (int i = 0; i < fieldCount; i++)
        {
            table.Columns.Add(reader.GetName(i), reader.GetFieldType(i));
        }
        table.BeginLoadData();
        object[] values = new object[fieldCount];
        while (reader.Read())
        {
            reader.GetValues(values);
            table.LoadDataRow(values, true);
        }
        table.EndLoadData();
        DataSet ds = new DataSet();
        ds.Tables.Add(table);
        return ds;
    } 



}
